package com.lanlian.cayman.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lanlian.cayman.R;
import com.lanlian.cayman.activity.TradeDetailsFundActivity;
import com.lanlian.cayman.adapter.FundDetailsPnlAdapter;
import com.lanlian.cayman.bean.EventFundDetailsFilterData;
import com.lanlian.cayman.utils.VolleyRequestUtils;
import com.lanlian.commonlib.base.BaseLazyFragment;
import com.lanlian.commonlib.bean.BaseResponse;
import com.lanlian.commonlib.bean.FundDetailsInfo;
import com.lanlian.commonlib.bean.ReqFundDetails;
import com.lanlian.commonlib.bean.RespFundDetails;
import com.lanlian.commonlib.event.EventCode;
import com.lanlian.commonlib.event.EventMessage;
import com.lanlian.commonlib.network.INetWorkCallback;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : huxiaobo@xlanlian.com
 *     date   : 2018/9/17 13:38
 *     desc   : 资金流水盈亏
 *     version: 1.0
 * </pre>
 */
public class FundDetailsPnlFragment extends BaseLazyFragment {

    private SmartRefreshLayout mLayRefresh;
    private RecyclerView mRecycleView;

    private ArrayList<FundDetailsInfo> mList = new ArrayList<>();
    private FundDetailsPnlAdapter mAdapter;

    private String mBeginDate;
    private String mEndDate;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_fund_details_pnl, null);
        initView(view);
        setListeners();
        initAdapter();
        return view;
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
        mLayRefresh = view.findViewById(R.id.lay_refresh);
        mRecycleView = view.findViewById(R.id.recycle_view);
    }

    @Override
    protected void setListeners() {
        super.setListeners();
        mLayRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                getFundDetails();
            }
        });
    }

    private void getFundDetails() {
        ReqFundDetails reqFundDetails = new ReqFundDetails();
        reqFundDetails.setBeginDate(mBeginDate);
        reqFundDetails.setEndDate(mEndDate);
        reqFundDetails.setSourceCurrency("USD");
        reqFundDetails.setType(TradeDetailsFundActivity.FUND_DETAILS_TYPE_PNL);
        VolleyRequestUtils.getInstance().getFundDetails(this.toString(), reqFundDetails,
                new INetWorkCallback() {
                    @Override
                    public void onSuccess(Object response) {
                        mLayRefresh.finishRefresh();
                        RespFundDetails respFundDetails = (RespFundDetails) response;
                        mList = respFundDetails.getDataList();
                        mAdapter.setNewData(mList);
                    }

                    @Override
                    public void onFail(BaseResponse baseResponse) {
                        mLayRefresh.finishRefresh();
                        showToast(baseResponse.getMsg());
                    }
                });
    }

    private void initAdapter() {
        mAdapter = new FundDetailsPnlAdapter(R.layout.adapter_fund_details_pnl, mList);
        mRecycleView.setAdapter(mAdapter);
        mRecycleView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mAdapter.bindToRecyclerView(mRecycleView);
        mAdapter.setEmptyView(R.layout.lay_empty_details);
    }

    @Override
    protected boolean isRegisteredEventBus() {
        return true;
    }

    @Override
    public void onReceiveEvent(EventMessage event) {
        super.onReceiveEvent(event);
        switch (event.getCode()) {
            case EventCode.EVENT_FUND_DETAILS_REFRESH:
                if (mIsUIVisible) {
                    mBeginDate = ((EventFundDetailsFilterData) event.getData()).getBeginDate();
                    mEndDate = ((EventFundDetailsFilterData) event.getData()).getEndDate();
                    mLayRefresh.autoRefresh();
                }
                break;
            default:
                break;
        }
    }

    @Override
    protected void loadData() {
        mLayRefresh.autoRefresh();
    }
}
